﻿using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YuFeng.Repositories;
using YuFeng.Repositories.Product;

namespace YuFeng.Services.Product
{

    public interface IProductCategoryService : IBaseService<Models.Product.ProductCategory>
    {
        IPagedList<Models.Product.ProductCategory> GetPageCategorys(string name, bool? isdelete, int page = 1, int size = 10);
        Models.Product.ProductCategory GetProductCategoryByID(string id);
    }
    public class ProductCategoryService : BaseService<Models.Product.ProductCategory>, IProductCategoryService
    {
        private readonly IProductCategoryRepository _productCategoryRepository;
        private readonly IUnitOfWork _unitOfWork;
        public ProductCategoryService(IProductCategoryRepository productCategoryRepository, IUnitOfWork unitOfWork)
            : base(productCategoryRepository, unitOfWork)
        {
            this._productCategoryRepository = productCategoryRepository;
            this._unitOfWork = unitOfWork;
        }
        public IPagedList<Models.Product.ProductCategory> GetPageCategorys(string name, bool? isdelete, int page = 1, int size = 10)
        {
            var query = _productCategoryRepository.GetAll();

            if (!string.IsNullOrWhiteSpace(name))
            {
                query = query.Where(e => e.Name.IndexOf(name) > -1);
            }
            if (isdelete.HasValue)
            {
                query = query.Where(e => e.IsDeleted == isdelete.Value);
            }

            return query.GetPage(o => o.Sort, page, size, false);
        }
        public Models.Product.ProductCategory GetProductCategoryByID(string id)
        {
            var query = _productCategoryRepository.GetById(id);
            return query;
        }
    }
}
